/**
 * -----------------------------------
 * Copyright (c) 2021-2025
 * All rights reserved, Designed By www.linfengtech.cn
 * 林风社交论坛商业版本请务必保留此注释头信息
 * 商业版授权联系技术客服	 QQ:  3582996245
 * 严禁分享、盗用、转卖源码或非法牟利！
 * 版权所有 ，侵权必究！
 * -----------------------------------
 */
package io.linfeng.modules.app.controller;

import cn.hutool.core.util.ObjectUtil;
import io.linfeng.common.annotation.NoRepeatSubmit;
import io.linfeng.common.utils.Result;
import io.linfeng.common.vo.PostDetailResponse;
import io.linfeng.common.vo.PostVipInfoResponse;
import io.linfeng.common.utils.AppPageUtils;
import io.linfeng.common.utils.R;
import io.linfeng.common.validator.ValidatorUtils;
import io.linfeng.modules.admin.entity.AppUserEntity;
import io.linfeng.modules.admin.entity.PostEntity;
import io.linfeng.modules.admin.service.PostService;
import io.linfeng.modules.app.annotation.Login;
import io.linfeng.modules.app.annotation.LoginUser;
import io.linfeng.modules.app.param.*;
import io.linfeng.modules.app.service.PostCollectionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;


/**
 * 
 * 帖子APP端
 * @author linfeng
 * @email 2445465217@qq.com
 * @date 2022-01-24 20:09:55
 */
@Api(tags = "移动端——帖子")
@RestController
@RequestMapping("app/post")
public class AppPostController {

    @Autowired
    private PostService postService;

    @Autowired
    private PostCollectionService postCollectionService;


    /**
     * 删除
     */
    @Login
    @PostMapping("/del")
    @ApiOperation("用户删除帖子")
    public Result delete(@RequestBody AddPostDeleteForm request,
                    @ApiIgnore @LoginUser AppUserEntity user){

		postService.del(request.getId(),user.getUid());

        return new Result();
    }

    @GetMapping("/detail")
    @ApiOperation("获取帖子详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "帖子id",paramType = "query", dataType = "Integer", required = true)
    })
    public Result<PostDetailResponse> detail(@RequestParam Integer id){

        PostDetailResponse response=postService.detail(id);
        return new Result<PostDetailResponse>().ok(response);
    }



    /**
     * 根据话题ID查询帖子列表分页
     */
    @PostMapping("/getPostListByDiscussId")
    @ApiOperation("根据话题ID查询帖子列表分页")
    public Result<AppPageUtils> getPostListByDiscussId(@RequestBody PostListForm request){

        AppPageUtils page = postService.getPostListByDiscussId(request);

        return new Result<AppPageUtils>().ok(page);
    }

    /**
     * 根据圈子ID查询帖子列表分页
     */
    @PostMapping("/getListByTopicId")
    @ApiOperation("根据圈子ID查询帖子列表分页")
    public Result<AppPageUtils> getListByTopicId(@RequestBody PostListForm request){

        AppPageUtils page = postService.getListByTopicId(request);

        return new Result<AppPageUtils>().ok(page);
    }

    /**
     * 根据用户ID查询帖子列表分页
     */
    @PostMapping("/getListByUid")
    @ApiOperation("根据用户ID查询帖子列表分页")
    public Result<AppPageUtils> getListByUid(@RequestBody PostListForm request){

        AppPageUtils page = postService.getListByUid(request);

        return new Result<AppPageUtils>().ok(page);
    }

    /**
     * 短视频列表分页
     */
    @Login
    @PostMapping("/videoList")
    @ApiOperation("短视频列表分页")
    public Result<AppPageUtils> videoList(@RequestBody VideoListForm request){
        AppPageUtils page = postService.queryShortVideoPageList(request);

        return new Result<AppPageUtils>().ok(page);
    }

    @PostMapping("/addReadCount")
    @ApiOperation("添加浏览量")
    public R addReadCount(@RequestBody AddReadCountForm request){
        postService.addReadCount(request.getPostId());

        return R.ok();
    }

    @Login
    @PostMapping("/addComment")
    @NoRepeatSubmit
    @ApiOperation("添加评论")
    public R addComment(@RequestBody AddCommentForm request, @ApiIgnore @LoginUser AppUserEntity user){
        ValidatorUtils.validateEntity(request);
        Integer value = postService.addComment(request, user);
        if(value==1){
            return R.ok().put("check",true);
        }
        return R.ok().put("check",false);
    }

    /**
     * 帖子点赞收藏
     */
    @Login
    @PostMapping("/addCollection")
    @NoRepeatSubmit
    @ApiOperation("帖子点赞收藏")
    public R addCollection(@RequestBody AddCollectionForm request, @ApiIgnore @LoginUser AppUserEntity user){
        postService.addCollection(request,user);

        return R.ok();
    }

    /**
     * 帖子取消点赞收藏
     */
    @Login
    @PostMapping("/cancelCollection")
    @NoRepeatSubmit
    @ApiOperation("帖子取消点赞收藏")
    public R cancelCollection(@RequestBody AddCollectionForm request, @ApiIgnore @LoginUser AppUserEntity user){
        postCollectionService.cancelCollection(request,user);
        return R.ok();
    }


    @Login
    @GetMapping("/joinTopicPost")
    @ApiOperation("用户加入圈子的动态列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页页码",paramType = "query", dataType = "Integer", required = true)
    })
    public Result<AppPageUtils> joinTopicPost(@RequestParam Integer page, @ApiIgnore @LoginUser AppUserEntity user){

        AppPageUtils pages =postService.joinTopicPost(page,user);
        return new Result<AppPageUtils>().ok(pages);
    }

    @GetMapping("/lastPost")
    @ApiOperation("最新动态列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页页码",paramType = "query", dataType = "Integer", required = true)
    })
    public Result<AppPageUtils> lastPost(@RequestParam Integer page){

        AppPageUtils pages =postService.lastPost(page);
        return new Result<AppPageUtils>().ok(pages);
    }

    @GetMapping("/lastPostByFilter")
    @ApiOperation("最新动态列表(按发布时间和评论时间)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页页码",paramType = "query", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "limit", value = "每页数量",paramType = "query", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "filter", value = "展示规则",paramType = "query", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "type", value = "帖子类型",paramType = "query", dataType = "Integer", required = true),
    })
    public Result<AppPageUtils> lastPostByFilter(@RequestParam Integer page, @RequestParam Integer limit, @RequestParam Integer filter, @RequestParam Integer type){

        AppPageUtils pages =postService.lastPostByFilter(page,limit,filter,type);
        return new Result<AppPageUtils>().ok(pages);
    }



    @Login
    @GetMapping("/followUserPost")
    @ApiOperation("获取关注用户帖子")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页页码",paramType = "query", dataType = "Integer", required = true)
    })
    public Result<Object> followUserPost(@RequestParam Integer page, @ApiIgnore @LoginUser AppUserEntity user){

        AppPageUtils pages =postService.followUserPost(page,user);
        if(ObjectUtil.isNull(pages)){
            return new Result<>().error("您没有关注的用户");
        }
        return new Result<>().ok(pages);
    }


    @Login
    @PostMapping("/addPost")
    @ApiOperation("发帖子")
    public R addPost(@RequestBody AddPostForm request, @ApiIgnore @LoginUser AppUserEntity user){
        ValidatorUtils.validateEntity(request);
        Integer id=postService.addPost(request,user);
        if(id==0){
            return R.error("发布帖子失败");
        }
        return R.ok().put("result",id);
    }

    @Login
    @GetMapping("/qrCode")
    @ApiOperation("分享二维码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "postId", value = "帖子id",paramType = "query", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "origin", value = "来源",paramType = "query",  required = true),
            @ApiImplicitParam(name = "url", value = "url分享路径",paramType = "query",  required = true)
    })
    public R qrCode(@RequestParam("postId") Integer postId,
                    @RequestParam("origin") String origin,
                    @RequestParam("url") String url,
                    @ApiIgnore @LoginUser AppUserEntity user) throws Exception {


        String resultUrl = postService.getSharePic(postId,origin,url,user);
        return R.ok().put("result",resultUrl);
    }

    @Login
    @GetMapping("/myCollectPost")
    @ApiOperation("我点赞收藏的帖子")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页页码",paramType = "query", dataType = "Integer", required = true)
    })
    public Result<AppPageUtils> myCollectPost(@RequestParam("page") Integer page, @ApiIgnore @LoginUser AppUserEntity user){

        AppPageUtils pages =postService.myCollectPost(page,user);
        return new Result<AppPageUtils>().ok(pages);
    }

    @Login
    @GetMapping("/myPost")
    @ApiOperation("我的帖子")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页页码",paramType = "query", dataType = "Integer", required = true)
    })
    public Result<AppPageUtils> myPost(@RequestParam("page") Integer page, @ApiIgnore @LoginUser AppUserEntity user){

        AppPageUtils pages =postService.myPost(page,user);
        return new Result<AppPageUtils>().ok(pages);
    }

    @GetMapping("/getPostListByType")
    @ApiOperation("帖子分类列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页页码",paramType = "query", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "type", value = "类型",paramType = "query", dataType = "Integer", required = true)
    })
    public Result<AppPageUtils> getPostListByType(@RequestParam("page") Integer page,
                                                  @RequestParam("type") Integer type){

        AppPageUtils pages =postService.getPostListByType(page,type);
        return new Result<AppPageUtils>().ok(pages);
    }


    @Login
    @GetMapping("/search")
    @ApiOperation("搜索帖子")
    @NoRepeatSubmit(lockTime = 2000)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页页码",paramType = "query", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "keyword", value = "搜索关键词",paramType = "query")
    })
    public Result<AppPageUtils> search(@RequestParam("page") Integer page,
                                       @RequestParam("keyword") String keyword){

        AppPageUtils pages =postService.search(page,keyword);
        return new Result<AppPageUtils>().ok(pages);
    }

    @Login
    @PostMapping("/setAdmin")
    @ApiOperation("设置圈子管理员")
    public Result setAdmin(@RequestBody SetAdminForm request, @ApiIgnore @LoginUser AppUserEntity user){
        Boolean setAdmin=postService.setAdmin(request,user);
        if(!setAdmin){
            return new Result().error("设置管理员失败");
        }
        return new Result();
    }

    @Login
    @PostMapping("/setPostTop")
    @ApiOperation("设置圈内置顶")
    public Result setPostTop(@RequestBody SetPostTopForm request, @ApiIgnore @LoginUser AppUserEntity user){
        Boolean setAdmin=postService.setPostTop(request,user);
        if(!setAdmin){
            return new Result().error("设置圈内置顶失败");
        }
        return new Result();
    }

    @Login
    @PostMapping("/topPostDel")
    @ApiOperation("解除圈内置顶")
    public Result topPostDel(@RequestBody SetPostTopForm request, @ApiIgnore @LoginUser AppUserEntity user){
        Boolean cancelAdmin=postService.topPostDel(request,user);
        if(!cancelAdmin){
            return new Result().error("解除圈内置顶失败");
        }
        return new Result();
    }

    @Login
    @PostMapping("/cancelAdmin")
    @ApiOperation("解除圈子管理员")
    public Result cancelAdmin(@RequestBody SetAdminForm request, @ApiIgnore @LoginUser AppUserEntity user){
        Boolean cancelAdmin=postService.cancelAdmin(request,user);
        if(!cancelAdmin){
            return new Result().error("解除圈子管理员失败");
        }
        return new Result();
    }

    @Login
    @PostMapping("/getVipPostInfo")
    @ApiOperation("获取用户是否付费")
    public Result<PostVipInfoResponse> getVipPostInfo(@RequestBody VipPostInfoForm request,@ApiIgnore @LoginUser AppUserEntity user){
        request.setUid(user.getUid());
        PostVipInfoResponse response=postService.getVipPostInfo(request);
        return new Result<PostVipInfoResponse>().ok(response);
    }


    @Login
    @PostMapping("/voteAdd")
    @ApiOperation("发起投票")
    public Result voteAdd(@RequestBody AddVoteForm request, @ApiIgnore @LoginUser AppUserEntity user){
        ValidatorUtils.validateEntity(request);
        Integer id=postService.voteAdd(request,user);
        if(id==0){
            return new Result().error("发起投票失败");
        }
        return new Result<>().ok(id);
    }

    @Login
    @PostMapping("/vote/userVote")
    @ApiOperation("用户投票")
    public Result userVote(@RequestBody UserVoteForm request,@ApiIgnore @LoginUser AppUserEntity user){
        postService.userVote(request,user);

        return new Result();
    }

    @GetMapping("/hotPost")
    @ApiOperation("帖子热榜")
    public R hotPost(){
        List<PostEntity> hotPost= postService.getHotPost();
        return R.ok().put("hotPost",hotPost);
    }

    @Login
    @PostMapping("/addArticle")
    @ApiOperation("发文章")
    public R addArticle(@RequestBody AddArticleForm request, @ApiIgnore @LoginUser AppUserEntity user){
        ValidatorUtils.validateEntity(request);
        Integer id=postService.addArticle(request,user.getUid());
        if(id==0){
            return R.error("发布文章失败");
        }
        return R.ok().put("result",id);
    }

}
